package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.query.NewsQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private NewsContentMapper contentMapper;

    @Override
    public void saveOrUpdate(News news) {
        //如果是发布状态,设置发布时间
        if(news.getState()==1)
        {
            news.setReleaseTime(new Date());
        }
        if(news.getId()!=null){
            newsMapper.updateByPrimaryKey(news);
            contentMapper.updateByPrimaryKey(news.getNewsContent());
        }else{
            newsMapper.insert(news);
            //文本对象关联主键id
            NewsContent content = news.getNewsContent();
            content.setId(news.getId());
            contentMapper.insert(content);
        }
    }

    @Override
    public NewsContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public News getById(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateBrowseNum(News news) {
        newsMapper.updateBrowseNum(news);
    }



    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<News> list = newsMapper.selectForList(qo);
        return new PageInfo(list);
    }

    /*全局查询*/
    @Override
    public PageInfo globalSearch(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        qo.setOrderBy("n.releaseTime DESC");
        List<News> list = newsMapper.query(qo);
        return new PageInfo(list);
    }

    @Override
    public int globalNumberSearch(SearchQueryObject qo) {
        return newsMapper.selectForCount(qo);
    }

}
